import {PageContainer} from "@ant-design/pro-components";
import GenerateTable from "@/pages/Generate/components/GenerateTable";
import {
  GenerateTableUpdateDTO,
  GenerateTableVO,
  saveGenerateTable,
  updateGenerateTable
} from "@/services/GenerateTableService";
import GenerateTableUpdateModal from "@/pages/Generate/components/GenerateTableUpdateModal";
import {useState} from "react";
import {Col, message, Row} from "antd";
import {GenerateColumnVO, getGenerateColumnList} from "@/services/GenerateColumnService";

const Generate = () => {
  // 编辑操作
  const [updateModalState, setUpdateModalState] = useState(false)
  const [currentGenerateTable, setCurrentGenerateTable] = useState<GenerateTableVO | undefined>()
  const [currentGenerateColumns, setCurrentGenerateColumns] = useState<GenerateColumnVO[] | undefined>()
  const updateEvent = async (record: GenerateTableVO) => {
    // 打开模态框
    setCurrentGenerateTable(record)
    let columns = await getGenerateColumnList({businessId: record.id});
    setCurrentGenerateColumns(columns)
    setUpdateModalState(true)
  }

  const saveEvent = async () => {
    // 打开模态框
    setUpdateModalState(true)
  }

  const onUpdateModalChange = async (state: boolean) => {
    if (state) {
      setUpdateModalState(state);

    } else {
      await setCurrentGenerateTable(undefined)
      setUpdateModalState(state)
    }
  }
  // 提交修改
  const onUpdateFinish = async (formData: GenerateTableUpdateDTO) => {
    let response
    if (currentGenerateTable) {
      formData.columns = currentGenerateColumns
      response = await updateGenerateTable(formData);
    } else {
      response = await saveGenerateTable(formData)
    }
    if (response) {
      message.success("操作成功")
      return true
    }
    return false;
  }

  /**
   * 生成代码事件
   * @param record
   */
  const generateEvent = (record: GenerateTableVO) => {

  }
  return <PageContainer title="代码生成器">
    {/*代码生成器表格 开始*/}
    <GenerateTable onSave={saveEvent} onUpdate={updateEvent} onEditColumn={generateEvent}/>
    {/*代码生成器表格 结束*/}

    {/*修改模态框 开始*/}
    <GenerateTableUpdateModal
      generateTable={currentGenerateTable}
      columns={currentGenerateColumns}
      open={updateModalState}
      onOpenChange={onUpdateModalChange}
      onColumnsChange={setCurrentGenerateColumns}
      onFinish={onUpdateFinish}/>
    {/*修改模态框 结束*/}




    <Row justify={'center'}>
      <Col span={6}>杨洋</Col>
      <Col span={6}>杨洋</Col>
      <Col span={6}>杨洋</Col>
      <Col span={6}>杨洋</Col>
    </Row>
  </PageContainer>
}

export default Generate
